package com.jingdun.sport.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jingdun.sport.authority.SecurityChecker;
import com.jingdun.sport.bean.model.ApiResult;
import com.jingdun.sport.bean.model.Room;
import com.jingdun.sport.bean.model.RoomAnswerRelation;
import com.jingdun.sport.bean.model.RoomUserRelation;
import com.jingdun.sport.security.util.SecurityUtils;
import com.jingdun.sport.service.RoomAnswerRelationService;
import com.jingdun.sport.service.RoomService;
import com.jingdun.sport.service.RoomUserRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/sport/answer/")
@Api(tags = "活动室答案关联关系接口")
public class AnswerController {

    @Autowired
    private RoomAnswerRelationService roomAnswerRelationService;

    @Autowired
    private SecurityChecker securityChecker;

    @Autowired
    private RoomUserRelationService roomUserRelationService;

    @Autowired
    private RoomService roomService;

    /**
     * 新建活动室答案关联关系接口
     */
    @PostMapping("/addRoomAnswerRelation")
    @ApiOperation(value = "新建活动室答案关联关系接口", notes = "新建活动室答案关联关系接口")
    public ResponseEntity<ApiResult> addRoomAnswerRelation(@RequestBody RoomAnswerRelation roomAnswerRelation) {
        roomAnswerRelation.setUserId(securityChecker.getUser().getUserId());
        roomAnswerRelationService.addRoomAnswerRelation(roomAnswerRelation);
//        判断是否房间内所有的人都已经领取宝箱。如果全部领取了就点击房间已经结束
        QueryWrapper<RoomUserRelation> roomUserRelationQueryWrapper = new QueryWrapper<>();
        roomUserRelationQueryWrapper
                .eq("room_id", roomAnswerRelation.getRoomId())
                .ne("all_done", 1);
        int count01 = roomUserRelationService.count(roomUserRelationQueryWrapper);
        QueryWrapper<RoomAnswerRelation> roomAnswerRelationQueryWrapper = new QueryWrapper<>();
        roomAnswerRelationQueryWrapper.eq("room_id", roomAnswerRelation.getRoomId());
        int count02 = roomAnswerRelationService.count(roomAnswerRelationQueryWrapper);
        if (count01 == count02) {
//            如果宝箱未完成人数等于答题人数，那么房间结束
            Room room = new Room();
            room.setId(roomAnswerRelation.getRoomId());
            room.setFinishedFlag("2");
            boolean update = roomService.updateById(room);
        }
        ApiResult apiResult = new ApiResult();
        return ResponseEntity.ok(apiResult);
    }

    /**
     * 获取活动室答案关联关系详情接口
     */
    @GetMapping("/getRoomAnswerRelationDetail")
    @ApiOperation(value = "获取活动室答案关联关系详情接口", notes = "获取活动室答案关联关系详情接口")
    public ApiResult getRoomAnswerRelationDetail(long roomId) {
        Map m = new HashMap<String, Object>();
        m.put("roomId", roomId);
        RoomAnswerRelation roomAnswerRelation = roomAnswerRelationService.getRoomAnswerRelationDetail(m);
        ApiResult apiResult = new ApiResult();
        apiResult.setData(roomAnswerRelation);
        return apiResult;
    }

    /**
     * 分页获取活动室答案关联关系接口
     */
    @RequestMapping("/getPaginationRoomAnswerRelation")
    @ApiOperation(value = "分页获取活动室答案关联关系接口", notes = "分页获取活动室答案关联关系接口")
    public ResponseEntity<ApiResult> getPaginationRoomAnswerRelation(long size, long current) {
        Map m = new HashMap<String, Object>();
        m.put("size", size);
        m.put("current", current);
        Page<Map<String, Object>> page = roomAnswerRelationService.getPaginationRoomAnswerRelation(m);
        //return ResponseEntity.ok(page);
        ApiResult apiResult = new ApiResult();
        apiResult.setData(page);
        return ResponseEntity.ok(apiResult);
    }

    /**
     * 修改活动室答案关联关系接口
     */
    @PostMapping("/updateRoomAnswerRelation")
    @ApiOperation(value = "修改活动室答案关联关系接口", notes = "修改活动室答案关联关系接口")
    public ResponseEntity updateRoomAnswerRelation(@RequestBody RoomAnswerRelation roomAnswerRelation) {
        roomAnswerRelationService.updateRoomAnswerRelation(roomAnswerRelation);
        ApiResult apiResult = new ApiResult();
        return ResponseEntity.ok(apiResult);
    }
}